package com.zhuangyuan.cloud.core.interceptors;

import com.zhuangyuan.cloud.core.annotations.DefaultLayout;
import com.zhuangyuan.cloud.core.models.NavModel;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 10191042 on 2019-11-19.
 */
public class LayoutInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        if(handler instanceof HandlerMethod){
            HandlerMethod method = (HandlerMethod)handler;
            if(method.hasMethodAnnotation(DefaultLayout.class)){
                List<NavModel> navList = new ArrayList<>();
                navList.add(new NavModel(false,"Menu","/menu/list"));
                navList.add(new NavModel(false,"Category","/category"));
                navList.add(new NavModel(false,"Nav1","https://www.baidu.com"));
                navList.add(new NavModel(false,"Nav2","https://www.baidu.com"));
                modelAndView.addObject("nav_list",navList);
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }

}
